<?php

namespace App\Http\Controllers;

use App\Http\Requests\Logic\Conference\QueryCollection;
use App\Http\Requests\Logic\Conference\QueryResource;
use App\Http\Requests\Logic\Conference\StoreResource;
use App\Http\Requests\Logic\Conference\UpdateResource;
use App\Models\Conference;
use App\Models\User;
use App\Notifications\ConferenceNotice;
use App\Repositories\ConferenceRepository;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;

class ConferenceController extends Controller
{
    protected $conferenceRepository;

    /**
     * ConferenceController constructor.
     * @param ConferenceRepository $conferenceRepository
     */
    public function __construct
    (
        ConferenceRepository $conferenceRepository
    )
    {
        $this->conferenceRepository = $conferenceRepository;
    }

    /**
     * 会议列表展示
     * @param QueryCollection $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryCollection $request)
    {
        $start_time = $request->get('start_time');
        $end_time = $request->get('end_time');
        $project = $request->get('project_name');
        $type = $request->get('type');
        $paginate = $request->get('paginate', 15);
        $status = $request->get('status');
        $query = $this->conferenceRepository->getByCompany(Auth::user()->company_id, Auth::user()->id ,$start_time, $end_time,$project, $type, $status);
        $result = collect($query->paginate($paginate)->items())->map(function ($item) {
            $item->projects = json_decode($item->projects);
            $item->attendees = json_decode($item->attendees);
            return $item;
        });
        return success($result);

    }

    /**
     * 发起会议
     * @param StoreResource $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(StoreResource $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $attributes['company_id'] = $request->user()->company_id;
        $attributes['initiator_id'] = $request->user()->id;
        $attributes['initiator'] = $request->user()->name;
        $projects_users = $attributes['attendee'];
        foreach($projects_users as $project){
            $projects[] = ['project_id' => $project['project_id'],'project_name' => $project['project_name']];
            foreach($project['users'] as $user){
                $attendees[] = ['user_id' => $user['user_id'],'user_name' => $user['user_name']];
                $users_id[] = $user['user_id'];
            }

        }
        $attendees = collect($attendees)->unique('user_id')->all();
        $users_id = collect($users_id)->unique()->all();
        $attributes['projects'] = json_encode($projects);
        $attributes['attendees'] = json_encode($attendees);
        $attributes = collect($attributes)->splice(1)->all();
//        dd($attributes);
        $status = $this->conferenceRepository->create($attributes);

        //通知信息
        $content[] = [
            "conference_id" => $status->id,
            "type" => "conference",
            "title" => "会议通知",
            "content" => $attributes['content'],
            "address" => $attributes['address'],
            "initiator" => $attributes['initiator']
        ];
        //写入通知
        Notification::send(User::query()->whereIn('id' , $users_id)->get() , new ConferenceNotice($content));
        return stored($status);
    }


    /**
     * 展示详情页
     * @param QueryResource $request
     * @param Conference $conference
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(QueryResource $request,Conference $conference)
    {
        $conference->projects = json_decode($conference->projects);
        $conference->attendees = json_decode($conference->attendees);
        $conference->images;
        return success($conference);
    }


    /**
     * 上传图片
     * @param Request $request
     * @param Conference $conference
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function upload(Request $request, Conference $conference)
    {
        $images = $request->file('images');
        $imagesRepository = new \App\Repositories\ImageRepository();
        $result = $imagesRepository->store($images,$conference->initiator_id, $conference->company_id ,0, $conference);
        return success($result);

    }

    /**
     * 修改说明
     * @param UpdateResource $request
     * @param Conference $conference
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(UpdateResource $request, Conference $conference)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $conference->update($attributes);
        return updated($conference);
    }

}
